/**
 *  Copyright (c) 2014 The xmlepf project.  All rights reserved.
 *
 *  This file is part of xmlepf (Event Driven Testing Framework).
 *
 *  xmlepf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  xmlepf is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with xmlepf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This software consists of voluntary contributions made by many
 *  individuals.  For exact contribution history, see the revision
 *  history and logs, available at http://code.google.com/p/xmlepf.
 */
package org.xmlepf.xml.adapter.saxon;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import net.sf.saxon.Configuration;
import net.sf.saxon.TransformerFactoryImpl;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.trans.XPathException;
import org.xmlepf.xml.adapter.XSLT;

/**
 *
 * 
 */
public class XSLTImp implements XSLT {

    private String xml;
    private String xsl;
    private StringBuilder transformationResult;
    private String transformerFactoryStr = "net.sf.saxon.TransformerFactoryImpl";
    private NodeInfo inputNodeXml;
    private NodeInfo inputNodeXsl;
    private TransformerFactoryImpl transformerFactory;
    private Transformer transformer;
    private List<ExtensionFunctionDefinition> extensions;

    public XSLTImp() {
        System.setProperty("javax.xml.transform.TransformerFactory", transformerFactoryStr);
        extensions = new LinkedList<ExtensionFunctionDefinition>();
        transformerFactory = new TransformerFactoryImpl();
    }

    @Override
    public void initialize(String xsl, String inputXml) throws Exception {
        //reset();
        this.xml = inputXml;
        this.xsl = xsl;
    
        loadXsl();
        loadXml();
        registerExtensions(extensions);
        transformer = transformerFactory.newTransformer(inputNodeXsl);
    }

    private void loadXsl() throws XPathException, UnsupportedEncodingException, Exception {
        StreamSource source = new StreamSource(new ByteArrayInputStream(xsl.getBytes("UTF-8")));
        Configuration config = transformerFactory.getConfiguration();
        inputNodeXsl = config.buildDocument(source);
    }

    private void loadXml() throws XPathException {
        StreamSource source = new StreamSource(new ByteArrayInputStream(xml.getBytes()));
        Configuration config = transformerFactory.getConfiguration();
        inputNodeXml = config.buildDocument(source);
    }

    private void reset() {
        transformer.reset();
        transformationResult.delete(0, transformationResult.length());
    }

    @Override
    public void registerExtensions(List<ExtensionFunctionDefinition> extensions) throws XPathException {
        for (ExtensionFunctionDefinition extension : extensions) {
            Configuration config = transformerFactory.getConfiguration();
            config.registerExtensionFunction(extension);
        }
    }

    @Override
    public String transform() throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        transformer.transform(inputNodeXml, new StreamResult(outputStream));
        transformationResult = new StringBuilder(xml.length()); // todo: investigate this value
        transformationResult.append(outputStream.toString("UTF-8"));
        return transformationResult.toString();
    }

    @Override
    public String getTransformationResult() {
        return transformationResult.toString();
    }
}
